/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package poi.UncertaintyCalculator;
import java.util.HashMap;

import masterAgent.DBmanager.DBConnection;
import poi.Info.Day;
import poi.Info.POI;
import poi.Info.Schedule;
import poi.Uncertainty.*;
import agent.Context.Goal;

/**
 * This class is used to calculate the Master agent Uncertainty for the parameter
 * Schedule
 * 
 * @author Hernani Costa
 * @version 0.1 date: 27/01/2013
 */

public class UncertaintyCalculatorForSchedule {
	HashMap<Integer, UncertaintySchedule> poiUncertaintySchedule;

	/**
	 * ------------------------------------------------------------------------
	 * Constructor class
	 * ------------------------------------------------------------------------
	 * 
	 * @param pois
	 *            - list with the POIs that we intend to calculate the uncertainty
	 */
	public UncertaintyCalculatorForSchedule(HashMap<Integer,POI> pois) {
		poiUncertaintySchedule = new HashMap<Integer, UncertaintySchedule>();
		calculates_Uncertainty_Knowledge_About_Schedule(pois);
	}

	/**
	 * Calculates the Uncertainty for the parameter Schedule, by considering the average of the list of DaysOn 
	 * 
	 * @param pois
	 *            - list with the POIs that we intend to calculate the uncertainty
	 */
	private void calculates_Uncertainty_Knowledge_About_Schedule(
			HashMap<Integer,POI> pois) {
		
		UncertaintySchedule uncertaintySchedule = null;
		for (POI poi : pois.values()) {
			HashMap<String, Day> day = poi.getDaysOn().getListDaysOn();

			int[] sch = new int[3];
			int count =0;
			if (day.size() > 0) {
				for (String temp : day.keySet()) {
					Schedule s = day.get(temp).getSchedule();
//					System.out.println("\t"+s.to_String() + " cout: "+s.getCount());
					count += s.getCount();
					sch[0] +=s.getSchedule()[0];
					sch[1] +=s.getSchedule()[1];
					sch[2] +=s.getSchedule()[2];
				}
			}
//			System.out.println("\nCOUNT = "+count);
//			System.out.println(sch[0]+ " "+sch[1]+ " " +sch[2]);
			
			
			uncertaintySchedule = new UncertaintySchedule((double) sch[0]/count, (double) sch[1]/count, (double) sch[2]/count);
//			System.out.println("\nNEW Uncertainty Schedule VALUES:\n\t\t "+ uncertaintySchedule.to_String());
			
//			System.out.println("\nDAY NAME: "+ dayName);
//			Schedule schedule = day.get(dayName).getSchedule();
//			System.out.println("Schedule: "+schedule.to_String());
//			// schedule in an array, example: "Morning"=0, "Afternoon"=1,
//			// "Night"=1
//			int[] scheduleArray = schedule.getSchedule();
//			val = 1.0 / schedule.getCount();
//			System.out.println("VAL:" + val +" \t Count: "+schedule.getCount());
//			surpriseSchedule = new SurpriseSchedule(scheduleArray[0] * val,
//					scheduleArray[1] * val, scheduleArray[2] * val);

			poiUncertaintySchedule.put(poi.getPoiID(), uncertaintySchedule);
		}
	}

	/**
	 * 
	 * @return a String with the Uncertainty values for the parameter Schedule, for
	 *         all POIs
	 */
	public String to_String() {
		String info = "";
		for (int poiID : poiUncertaintySchedule.keySet()) {
			info += "poiID: " + poiID + "\n\t"
					+ poiUncertaintySchedule.get(poiID).to_String() + "\n";
		}
		return info;
	}

	// ------------------------------------------------------------------------
	// ------------------------GETs and SETs-----------------------------------
	// ------------------------------------------------------------------------

	public HashMap<Integer, UncertaintySchedule> getPoiUncertaintySchedule() {
		return poiUncertaintySchedule;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Only for debug purpose
	 * ------------------------------------------------------------------------
	 */
	public static void main(String args[]) {
		// DBManager manager = new DBManager();
		DBConnection connector = new DBConnection();

		POI poi = connector.getPOI(1507669);
		// System.err.println("O pesquisado\n" + poi.to_String() + "\n\n");

		HashMap<Integer,POI> pois = connector.getPOIsNear(poi.getLocation(),
				200, new Goal().getCoffee());
		// for (POI p : pois) {
		// System.err.println(p.to_String());
		// }

		UncertaintyCalculatorForSchedule m = new UncertaintyCalculatorForSchedule(pois);
		System.out.println(m.to_String());
	}

}
